package com.jstyle.jclife.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.jstyle.jclife.R;
import com.jstyle.jclife.utils.SharedPreferenceUtils;

public class LoadingActivity extends Activity {
    Handler handler;
    private Class<? extends BaseActivity> nextActivity;
    private static final String TAG = "LoadingActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        }
        setContentView(R.layout.activity_loading);
        Log.i(TAG, "onCreate: ");
        handler = new Handler();
//        nextActivity = null;
        boolean isLogin= SharedPreferenceUtils.getBoolean(SharedPreferenceUtils.KEY_LOGIN,false);
        boolean isFirst= SharedPreferenceUtils.getBoolean(SharedPreferenceUtils.KEY_Is_First,true);
        if (isFirst) {
            nextActivity = BindDeviceActivity.class;
            showPrivacy();
            return;
        } else {
            nextActivity = isLogin?MainActivity.class:LoginActivity.class;
           // nextActivity = MainActivity.class;
       }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(LoadingActivity.this,nextActivity);
                startActivity(intent);
                finish();
            }
        }, 1500);
    }

    int requestCode=222;
    private void showPrivacy() {
        Intent intent=new Intent(this,ProvicyActivity.class);
        startActivityForResult(intent,requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==this.requestCode&&resultCode==RESULT_OK){
            Intent intent=new Intent(LoadingActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
